package com.railway.dao.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.railway.bean.Expenditures;
import com.railway.bean.Integra;
import com.railway.bean.IntegraDetailed;
import com.railway.bean.Revenue;
import com.railway.dao.IntegraDao;
import com.railway.util.DBUtil;

public class IntegraDaoImpl implements IntegraDao {

	JdbcTemplate jt = new JdbcTemplate(DBUtil.getDataSource());
	DateFormat dft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

	@Override
	public Integra selectIntegra(int user_id) {
		String sql = "select * from integra where user_id = ?";
		List<Integra> list = jt.query(sql,new BeanPropertyRowMapper<Integra>(Integra.class),user_id);
		if(list.size() == 0) {
			return null;
		}else {
			return list.get(0);
		}
	}

	@Override
	public List<IntegraDetailed> selectIntegraDetailedsByTime(int integra_id,String give_or_take,String Startingtime,String Endtime) {
		String sql = "";
		if("+".equals(give_or_take) || "-".equals(give_or_take)) {
			sql = "select * from integraDetailed where integra_id = ? and give_or_take=? and create_at between ? and ? create_at DESC";
			return jt.query(sql, new BeanPropertyRowMapper<IntegraDetailed>(IntegraDetailed.class), integra_id,give_or_take,Startingtime,Endtime);
		}else {
			sql = "select * from integraDetailed where integra_id = ? and create_at between ? and ? create_at DESC";
			return jt.query(sql, new BeanPropertyRowMapper<IntegraDetailed>(IntegraDetailed.class), integra_id,Startingtime,Endtime);
		}
		
	}

	@Override
	public List<Revenue> selectRevenue(int integra_id, int PageNum, int PageSize, String oldTime1, String oldTime2) {
		String sql = "select * from revenue where revenue_id = ? and (time between ? and ??) limit ?,?";
		return jt.query(sql, new BeanPropertyRowMapper<Revenue>(Revenue.class), dft.format(oldTime1),dft.format(oldTime1),PageNum,(PageSize-1)*PageSize);
	}

	@Override
	public List<Expenditures> selectExpenditures(int integra_id, int PageNum, int PageSize, String oldTime1,String oldTime2) {
		String sql = "select * from revenue where expenditures_id = ? and (time between ? and ??) limit ?,?";
		return jt.query(sql, new BeanPropertyRowMapper<Expenditures>(Expenditures.class), oldTime1,oldTime1,PageNum,(PageSize-1)*PageSize);
	}

	@Override
	public int updateIntegra(int integra_id,int countImtegral,int Number_of_credits_aboutTo_expire) {
		String sql = "update integra set countImtegral = ?,Number_of_credits_aboutTo_expire=? where integra_id = ?";
		return jt.update(sql,countImtegral,Number_of_credits_aboutTo_expire,integra_id);
	}

	@Override
	public int insertIntegra(int user_id) {
		String sql = "insert into integra(user_id,countImtegral) values(?,0)";
		return jt.update(sql, user_id);
	}

	@Override
	public List<IntegraDetailed> selectIntegraDetaileds(int integra_id) {
		String sql ="select * from integraDetailed where integra_id = ? and create_at between ? and ? create_at DESC";
		
		return jt.query(sql, new BeanPropertyRowMapper<IntegraDetailed>(IntegraDetailed.class), integra_id);
	}


}
